import {
  selectList,
  selectDetails,
  selectPapersDetails,
  driverOperate,
  paperOperate,
  selectDictByCode,
  ableDriverOperate,
} from '@/services/tmsDriver';
import prompt from '@/components/Prompt';

export default {
  namespace: 'tmsDriver',

  state: {
    selectList: {},
    selectDetails: {},
    detailsPaper: {},
    papersList: {},
  },

  effects: {
    *selectList({ payload, callback }, { call, put }) {
      const response = yield call(selectList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'show',
          payload: {
            selectList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *selectDetails({ payload, callback }, { call, put }) {
      const response = yield call(selectDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            selectDetails: { [payload.id]: response.data },
            papersList: {
              [payload.id]: {
                list: response.data.details,
              },
            },
          },
        });
        callback && callback(response.data);
      }
    },
    *selectPapersDetails({ payload, callback }, { call, put }) {
      const response = yield call(selectPapersDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'papersDetails',
          payload: {
            detailsPaper: response.data,
          },
        });
        callback && callback(response.data);
      }
    },
    *driverOperate({ payload, callback }, { call }) {
      const response = yield call(driverOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *paperOperate({ payload, callback }, { call, put }) {
      const response = yield call(paperOperate, payload);
      if (response.code === 0) {
        prompt({ content: response.message });
        callback && callback(response.data);
      }
    },
    *selectDictByCode({ payload, callback }, { call, put }) {
      const response = yield call(selectDictByCode, payload);
      callback(response);
    },
    *ableDriverOperate({ payload, callback }, { call }) {
      const response = yield call(ableDriverOperate, payload);
      const message = payload.type ? `启用${response.message}` : `禁用${response.message}`;
      if (response.code === 0) {
        prompt({ content: message });
        callback(response);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        selectList: payload.selectList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        selectDetails: { ...state.selectDetails, ...payload.selectDetails },
        papersList: { ...state.papersList, ...payload.papersList },
      };
    },
    papersDetails(state, { payload }) {
      return {
        ...state,
        detailsPaper: payload.detailsPaper,
      };
    },
  },
};
